package com.etc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.etc.entity.Career;
import com.etc.entity.CareerClassify;
import com.etc.entity.Skill;
import com.etc.tools.DBTools;

public class CareerDAO {

	DBTools tool = new DBTools();
	Connection conn = tool.getConnection();
	SkillDAO skillDao = new SkillDAO();
	CareerClassifyDAO classDao = new CareerClassifyDAO();
	
	//查找一个职业的全部属性，根据id
	public Career findCareerById(String careerId) {
		String sql = "select * from t_career where career_id = ?";
		PreparedStatement ps = null;
		Career career = null;
		CareerClassify classify = null;
		ArrayList<Skill> skills = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, careerId);
			ResultSet set = ps.executeQuery();
			set.next();
			classify = classDao.findClassById(set.getString("class_id"));
			//根据职业类别id，返回五个本职业的技能
			skills = skillDao.findSkillByClass(classify.getClassId());
			career = new Career(set.getString("career_id"), classify, 
					set.getInt("start_heal"), set.getInt("start_image"), set.getInt("start_attack"),
					set.getInt("start_critical"), set.getInt("start_defense"), set.getInt("start_speed"),
					skills);
			
		} catch (SQLException e) {
			System.out.println("根据id获取职业分类对象查询异常");
			e.printStackTrace();
		}
		return career;
	}
	
	   //查找一个职业的全部属性，根据类别Id
	   //创建人物的时候，由于人物的初始属性是根据职业的基础属性+装备属性
	   //这个方法是为了找到职业的初始属性，classId来源于创建人物时选择的职业
		public Career findCareerByClassId(String classId) {
			String sql = "select * from t_career where CLASS_ID = ?";
			PreparedStatement ps = null;
			Career career = null;
			CareerClassify classify = null;
			ArrayList<Skill> skills = new ArrayList<>();
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, classId);
				ResultSet set = ps.executeQuery();
				set.next();
				classify = classDao.findClassById(classId);
				skills = skillDao.findSkillByClass(classify.getClassId());
				career = new Career(set.getString("career_id"), classify, 
						set.getInt("start_heal"), set.getInt("start_image"), set.getInt("start_attack"),
						set.getInt("start_critical"), set.getInt("start_defense"), set.getInt("start_speed"),
						skills);
				
			} catch (SQLException e) {
				System.out.println("根据id获取职业分类对象查询异常");
				e.printStackTrace();
			}
			return career;
		}
}
